#include "Enemies.hh"
#include <iostream>
#include <cstdlib>
#include <ctime>

Enemy::Enemy(const std::string& name, const std::string& desc,
    unsigned health, unsigned attack, unsigned defense)
    : Character(name, desc, health, attack, defense) {
}

std::string Enemy::toString() const {
    return name + " (Health: " + std::to_string(currentHealth) + ")";
}

// Goblin implementation
Goblin::Goblin()
    : Enemy("Goblins", "A dwarf, but ferocious creature, armed with a rusty dagger。", 10, 3, 1) {
}

// Skeleton implementation
Skeleton::Skeleton()
    : Enemy("Skeleton Warrior", "An undead skeleton armed with a worn-out sword and shield。", 15, 4, 3) {
}

// Wizard realization
Wizard::Wizard()
    : Enemy("Boss Evil Sorcerer", "A powerful dark wizard who is the master of this dungeon。", 30, 7, 4) {
}

bool Wizard::attack(ICharacter& target) {
    // Decide at random which attack to use
    std::srand(static_cast<unsigned>(std::time(nullptr)));
    int attackType = std::rand() % 3;

    if (attackType == 0) {
        // Normal Attack
        std::cout << name << "Cast a Shadow Arrow！" << std::endl;
        return target.takeDamage(getAttack());
    }
    else if (attackType == 1) {
        // Fireball - Deals more damage
        std::cout << name << "Cast a powerful fireball spell！" << std::endl;
        return target.takeDamage(getAttack() + 3);
    }
    else {
        // Life Drain - Deals damage and restores health
        std::cout << name << "Cast Life Drain！" << std::endl;
        bool killed = target.takeDamage(getAttack() + 1);
        currentHealth = std::min(baseHealth, currentHealth + 2);
        std::cout << name << "Restored 2 health！" << std::endl;
        return killed;
    }
}

// Troll realization
Troll::Troll()
    : Enemy("Hill trolls", "A monster with a huge size and strong regeneration ability", 40, 8, 5) {
}

bool Troll::attack(ICharacter& target) {
    // Trolls have a 25% chance to deal double damage
    if (rand() % 100 < 25) {
        std::cout << name << "Enraged, dealing double damage！" << std::endl;
        return target.takeDamage(getAttack() * 2);
    }
    return target.takeDamage(getAttack());
}

// Ghost fulfillment
Ghost::Ghost()
    : Enemy("Wraiths", "A translucent specter of horror", 25, 6, 2) {
}

bool Ghost::takeDamage(unsigned damage) {
    // Spectres have a 50% chance to dodge attacks
    if (rand() % 100 < 50) {
        std::cout << name << "Blurred, dodging the attack！" << std::endl;
        return false;
    }
    return Enemy::takeDamage(damage);
}

// Dragon realization
Dragon::Dragon()
    : Enemy("Red Dragon", "An ancient dragon that breathes flames", 50, 12, 8) {
}

bool Dragon::attack(ICharacter& target) {
    // Dragons have a 30% chance to use Fire Breath
    if (rand() % 100 < 30) {
        std::cout << name << "Blazing flames erupted！" << std::endl;
        return target.takeDamage(getAttack());
    }
    return target.takeDamage(getAttack());
}

// The Dark Knight realized
DarkKnight::DarkKnight()
    : Enemy("The Dark Knight", "A cursed undead knight", 35, 10, 7) {
}

bool DarkKnight::attack(ICharacter& target) {
    // The Dark Knight's attacks have a lifesteal effect
    bool killed = target.takeDamage(getAttack());
    if (!killed) {
        currentHealth = std::min(baseHealth, currentHealth + 3);
        std::cout << name << "Absorbs 3 health from an attack！" << std::endl;
    }
    return killed;
}


//敌人管理系统：
//代码实现了一个多样化的敌人系统，通过重写 attack() 和 takeDamage() 实现不同行为模式。Boss级敌人（如巫师、龙）拥有复杂技能，普通敌人通过概率机制增加战斗变数。
//例如：邪恶巫师：每次攻击随机选择三种法术之一（暗影箭，火球术，生命汲取）。  幽灵：50%物理免疫。   等等


//Enemy Management System:
// The code implements a diverse enemy system by overriding the attack() and takeDamage() methods to achieve different behavior patterns. 
// Boss-level enemies (such as wizards and dragons) possess complex skills, while ordinary enemies add combat variability through probability mechanisms.
// For example: Evil Wizard: Randomly selects one of three spells (Shadow Arrow, Fireball, Life Drain) for each attack. Ghost: 50% physical immunity. And so on.